package com.cn.mmd.controller;

import com.cn.mmd.model.CourseStuAttendance;
import com.cn.mmd.model.StudentApp;
import com.cn.mmd.model.StudentCount;
import com.cn.mmd.service.CourseStuAttendanceService;
import com.cn.mmd.service.StudentCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/2.
 */
@Controller
@RequestMapping(value = "page/studentCount")
public class StudentCountController {

    @Autowired
    private StudentCountService studentCountService;

    @Autowired
    private TokenController tokenController;

    @Autowired
    private CourseStuAttendanceService courseStuAttendanceService;

    @RequestMapping(value = "/selectStudent")
    public synchronized
    @ResponseBody
    Object selectStudent(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        String token = request.getParameter("token");
        String time = request.getParameter("time");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            StudentCount studentCount = studentCountService.selectByPrimaryKey(studentId);
            if (studentCount != null) {
                int num;
                CourseStuAttendance courseStuAttendance;
                Map<String, Object> objectMap1 = new HashMap<String, Object>();
                objectMap1.put("activityAddNum", studentCount.getActivityaddnum());
                objectMap1.put("answerEvaluateNum", studentCount.getAnswerevaluatenum());
                objectMap1.put("answerIssueNum", studentCount.getAnswerissuenum());
                objectMap1.put("answerQuestionnaireNum", studentCount.getAnswerquestionnairenum());
                objectMap1.put("issueAddNum", studentCount.getIssueaddnum());
                Map<String, Object> objectMap2 = new HashMap<String, Object>();
                courseStuAttendance = new CourseStuAttendance();
                courseStuAttendance.setAttendancetime(time);
                courseStuAttendance.setStuid(studentId);
                courseStuAttendance.setAttendancecontent("0");
                if (courseStuAttendanceService.countByCouIdStuId(courseStuAttendance) > 0) {
                    num = courseStuAttendanceService.countByCouIdStuId(courseStuAttendance);
                    objectMap2.put("Num", num);
                } else {
                    objectMap2.put("Num", "0");
                }
                courseStuAttendance.setAttendancecontent("1");
                if (courseStuAttendanceService.countByCouIdStuId(courseStuAttendance) > 0) {
                    num = courseStuAttendanceService.countByCouIdStuId(courseStuAttendance);
                    objectMap2.put("LateNum", num);
                } else {
                    objectMap2.put("LateNum", "0");
                }
                courseStuAttendance.setAttendancecontent("2");
                if (courseStuAttendanceService.countByCouIdStuId(courseStuAttendance) > 0) {
                    num = courseStuAttendanceService.countByCouIdStuId(courseStuAttendance);
                    objectMap2.put("leaveEarlyNum", num);
                } else {
                    objectMap2.put("leaveEarlyNum", "0");
                }
                courseStuAttendance.setAttendancecontent("3");
                courseStuAttendance.setAttendancetime(time);
                courseStuAttendance.setStuid(studentId);
                if (courseStuAttendanceService.countByCouIdStuId(courseStuAttendance) > 0) {
                    num = courseStuAttendanceService.countByCouIdStuId(courseStuAttendance);
                    objectMap2.put("truantNum", num);
                } else {
                    objectMap2.put("truantNum", "0");
                }

                courseStuAttendance.setAttendancecontent("4");
                if (courseStuAttendanceService.countByCouIdStuId(courseStuAttendance) > 0) {
                    num = courseStuAttendanceService.countByCouIdStuId(courseStuAttendance);
                    objectMap2.put("leaveNum", num);
                } else {
                    objectMap2.put("leaveNum", "0");
                }
                objectMap1.put("countNum",objectMap2);
                objectMap.put("data", objectMap1);
                objectMap.put("message", "成功");
                objectMap.put("code", 200);
            } else {
                objectMap.put("message", "此用户没有操作数据");
                objectMap.put("code", 201);
            }
        } else {
            objectMap.put("message", "此用户没有权限做此操作");
            objectMap.put("code", 202);
        }
        return objectMap;
    }

    public synchronized Object add(StudentCount studentCount,int n) {
        if(n==1) {
            int num = studentCountService.updateByPrimaryKeySelective(studentCount);
            if (num > 0) {
                return num;
            }
        }else {
            int num = studentCountService.insertSelective(studentCount);
            if (num > 0) {
                return num;
            }
        }
        return null;
    }
}
